var $$ = {
    unittest : false,
    debug: true,

    isEmpty: function(string) {
        if (string === undefined || string === null) {
            return true;
        }
        if (typeof string !== 'string') {
            return false;
        }
        if (string && string.length > 0) {
            return false;
        }
        return true;
    },
    isNotEmpty: function(string) {
        return !$$.isEmpty(string);
    },
    isNullOrUndef: function(object) {
        return (object === null) || (object === undefined);
    },

    /**
     * @param message {string}
     */
    log: function(message) {
        if (console) {
            console.log(message);
        }
    },
    /**
     * @param condition {boolean}
     * @param message {string}
     */
    assert: function(condition, message) {
        //
    },
    /**
     * @param message {string}
     */
    alert: function(message) {
        if (!this.unittest) {
            alert(message);
        }
    },
    /**
     * @param options {object}
     */
    ajax: function(options) {
        //
        if (options.url.indexOf('?') >= 0) {
            options.url = options.url + "&ts=" + Date.now();
        } else {
            options.url = options.url + "?ts=" + Date.now();
        }
        if (options.async && ($.url.attr('port') === '8888' || $.url.attr('port') === '8080')) {
            $$.log('use async=false on development.:' + options.url)
            options.async = false;
        }

        var retry = 3;
        var _ajax = function() {
            $$.log('request...' + options.url);
            retry--;
            $.ajax(options);
        };

        var _success = options.success;
        options.success = function(data, dataType) {
            if (data['status'] && data['status'] === 'APPENGINE' && retry !== 0) {
                _ajax(options);
                $$.log('retrying...' + options.url);
            } else if (_success) {
                _success(data, dataType);
            }
        };

        if ($$.qunit && $$.qunitmock) {
            var mockresponse = $$.qunitmock(options);
            if ($$.isNullOrUndef(mockresponse.status)) {
                options.success(mockresponse, 'json');
                if (options.complete) {
                    options.complete(null, 'success');
                }
            } else {
                options.error(null, mockresponse.status, mockresponse.error);
                if (options.complete) {
                    options.complete(null, 'error');
                }
            }
        } else {
            _ajax(options);
        }
    }
};
